<?php
class MainController extends Controller {
	
	function MainAction()
	{
		//取得主菜的名
		$this->data["menu_list"] = FrameworkModel::getInstance()->getMenusList($this->user['id']);

		//数据输出
		$this->JS['img']= STATIC_PATH."/images/main/";
		$this->data['img']= STATIC_PATH."/images/main/";
		$this->data['ico']= STATIC_PATH."/images/ico/";

		$this->view("Main","main");
	}

	function LoginAction(){
		if(strtoupper(CONTROLLER) == "MAIN")
		{
			$this->JS[code]="no";
			$this->data["username"] = P("username","");
			$this->data["password"] = P("password","");
		
			$verification = P("verification","");
			$this->view("Login","index");
		}else{
			echo '<script type="text/javascript">';
			echo 'window.top.location="/"';
			echo '</script>';
		}
	}

	function SubmitAction(){

		//取得用户数据
		$username = P("username","");
		$password = P("password","");
		$verification = P("verification","");
		
		//数据下传
		$this->data["username"] = $username;
		$this->data["password"] = $password;
		
		if($verification != $_SESSION["sessionRound"]){
			$this->JS[code]="ng";
			$this->view("Login","index");
		}else{
			$user_rs = FrameworkModel::getInstance()->getUser($username,$password);

			//判断用户是否存在
			if($user_rs){
				$this->_attendance($user_rs);
				ISession::set("uid",$user_rs["id"]);
				$this->redirect("/");
			}else{
				$this->JS[code]="error";
				$this->view("Login","index");
			}
		}
	}
	
	function VerificationAction(){
		//生成验证码图片
    	session_start();
        Header("Content-type: image/PNG"); 
  		srand((double)microtime()*1000000); 
  		$roundNum=rand(1000,9999);
		//把随机数存入session以便以后用
		$_SESSION["sessionRound"]=$roundNum;
        $im = imagecreate(65,21);

        $blue = ImageColorAllocate($im,187,230,247);
        $red = ImageColorAllocate($im,255,0,0);
        $black = ImageColorAllocate($im, 0,0,255);

 		//局域填充，相当于背景
        imagefill($im,68,25,$blue);
   		//将四位整数验证码绘入图片
        imagestring($im,5,15,3,$roundNum,$red);
        
        for($i=0;$i<600;$i++)   //加入干扰象素
        {
			imagesetpixel($im,rand()%70,rand()%80 ,$black);
        }
        ImagePNG($im);
        ImageDestroy($im);
	}
	
	function ExitAction(){
		
		//清楚SESSION
		ISession::fre("uid");
		
		//画面重定向
		$this->redirect("/main/login");
	}
	
	function _attendance($user_rs)
    {
        $date = date("Y-m-d");
        $time = date("H:i:s");
        /* 获取员工信息 */
        $staff = StaffModel::getInstance()->getByRow('login_id='.$user_rs['id']);
        if (!$staff) {
            return false;
        }

        /* 获取考勤特殊安排 */
        $attarrange = AttendanceArrangeModel::getInstance()->getList();
        if (!$attarrange) {
            return false;
        }

        $attendance = AttendanceModel::getInstance()->getByRow("staff_id=".$staff['id']." and attendance_date='".$date."'");

        /** 判断今天是否考勤 **/
        if ($attendance['attendance_time_start'] && $attendance['attendance_time_end']) {
            return false;
        }

        /** 上班考勤 **/
        if (!$attendance['attendance_time_start'] && !$attendance['attendance_time_end']) {
            $data = array(
                'staff_id'              => $staff['id'],
                'attendance_date'       => $date,
                'attendance_time_start' => $time,
                'created'               => time(),
            );
            AttendanceModel::getInstance()->add($data);
        }

        /** 下班考勤 **/
        if ($attendance['attendance_time_start'] && !$attendance['attendance_time_end']) {
            $data = array('attendance_time_end' => $time);
            AttendanceModel::getInstance()->edit($data, $attendance['id']);
        }
//        foreach ($attarrange as $item) {
//            $date = $item['year'].'-'.$item['month'].'-'.$item['day'];
//            if ($data == date("Y-m-d")) {
//
//            }
//        }
    }
}